<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta charset="utf-8"/>
    <title>zlt</title>
    <script type="text/javascript" src="js/jquery-3.2.1.min.js"></script>
    <script type="text/javascript" src="js/sso.js"></script>
</head>
<body>
<div>
    <p>登录名：<span id="loginName"></span></p>
    <p>应用id：<span id="clientId"></span></p>
    <p>token：<span id="accessToken"></span></p>
    <p><input type="button" value="登出" onclick="logout()"/></p>
</div>
<script>
    function login() {
        sessionStorage.setItem("visitUri", window.location.href);
        window.location = getAuthorizeUri();
    }

    window.onload = function() {
        let accessToken = sessionStorage.getItem('access_token');
        if (accessToken) {//已登录
            //获取token和用户信息
            $.ajax({url:'http://127.0.0.1:'+port+'/user?access_token='+accessToken, success:function(result) {
                if (result) {
                    $('#accessToken').html(accessToken);
                    $('#loginName').html(result);
                    $('#clientId').html(clientId);
                } else {
                    login();
                }
            }});
        } else {//未登录
            login();
        }
    };

    function logout() {
        let accessToken = sessionStorage.getItem('access_token');
        sessionStorage.removeItem('access_token');
        window.location = getLogoutUri(accessToken);
    }
</script>
</body>
</html>
